﻿using System;
using Castle.ActiveRecord;
using NHibernate;
using System.Collections.Generic;
using Castle.ActiveRecord.Queries;
using Castle.ActiveRecord.Framework;

using NHibernate.Criterion;




namespace ContactManager.Models.Entities
{
  [ActiveRecord("Contact")]
    public class Contact //: ActiveRecordBase<Contact>

	{

        private System.Guid _Id;
		private System.DateTime?_DateEntered;//auto in the db
        private System.Guid _GroupId;
		private string _FirstName;
		private string _LastName;
		private string  _Phone;
		private string _Email;
        private byte[] version;
        private Int16? _Age;
        private ContactGroup group;
        //private Int32 version;

        #region Properties
        //GroupId is the name of the column in the Contact Table
        //Set insert and update false OTHERWISE it is passed as an extra parameter
        //very naughty
        
        [BelongsTo("GroupId",Insert=false,Update=false)]
        public ContactGroup ContactGroup
        {
            get { return group; }
            set { group = value; }
        }
   		
		public Contact()
		{
		}

        [PrimaryKey(PrimaryKeyType.Assigned)]
        public System.Guid Id
		{
			get{return this._Id;}
			set{_Id = value;}
		}

      [Property(Insert = false, Update = false)]
      public System.DateTime? DateEntered
		{
			get{return this._DateEntered;}
			set	{this._DateEntered = value;}
		}

     
        [Property(Unique = false, NotNull = true)]
        public string FirstName
		{
			get{return this._FirstName;}
			set{this._FirstName = value;}
		}

        [Property]
		public string LastName
		{
			get{return this._LastName;}
			set{this._LastName = value;}
		}

        [Property]
		public string Phone
		{
			get{return this._Phone;}
			set{this._Phone = value;}
    	}

        [Property]
        public string Email
		{
			get{return this._Email;}
			set{this._Email = value;}
		}

        
        [Property(NotNull=false)]
        public Int16? Age
        {
            get { return this._Age; }
            set { this._Age = value; }
        }

        [Property]
        public System.Guid GroupId
        {
            get { return this._GroupId; }
            set { this._GroupId = value; }
        }

        //[Version("Version")]
        //public byte[] Version
        //{
        //    get { return version; }
        //    set { version = value; }
        //}

        #endregion

        #region GLOBAL MEthods
      /*
        public static Contact[] GetContacts()
        {
            return ActiveRecordBase<Contact>.FindAll();
        }
        public static Contact[] GetContactsByGroup(Guid groupId)
        {
            return ActiveRecordBase<Contact>.FindAllByProperty("GroupId", groupId);
        }
        public static Contact GetContactById(Guid guid)
        {
            return FindByPrimaryKey(guid);
            /*
             * return FindOne(Expression.Eq("Id",guid));
            
            return (Contact)  ActiveRecordMediator.Execute(
              delegate(ISession session, object instance)
              {
                  // create the query...
                  IQuery query = session.CreateQuery("from Contact p where p.Contact.Id equal :guid");

                  // set the parameters...
                  query.SetString("guid", (String)instance);

                  // fetch the results...
                  Contact results = query.Contact();

                  // OPTIONAL: convert the results to an array or 
                  // something meaningful, instead of returning the IList
                  // Contact[] contacts = new Contact[results.Count];
                  //results.CopyTo(contacts, 0);

                  // return
                  return results;
              }, guid);
             

        }
        public static IList<Contact> GetContactsLastName(String initial)
        {
            return (IList<Contact>)Execute(
               delegate(ISession session, object instance)
               {
                   // create the query...
                   IQuery query = session.CreateQuery("from Contact p where p.Contact.LastName like :initial");

                   // set the parameters...
                   query.SetString("initial", (String)instance);

                   // fetch the results...
                   IList<Contact> results = query.List<Contact>();

                   // OPTIONAL: convert the results to an array or 
                   // something meaningful, instead of returning the IList
                  // Contact[] contacts = new Contact[results.Count];
                   //results.CopyTo(contacts, 0);

                   // return
                   return results;
               }, initial);
        }
        public static int CountAllContacts()
        {
            return Count(typeof(Contact));
        }
      */
        #endregion

    }
}